package cn.iocoder.yudao.module.call.controller.admin.stat;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
import cn.iocoder.yudao.module.system.api.tenant.TenantApi;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.call.controller.admin.stat.vo.*;
import cn.iocoder.yudao.module.call.service.stat.StatService;

@Tag(name = "管理后台 - 外呼统计报表")
@RestController
@RequestMapping("/call/stat")
@Validated
public class StatController {

    @Resource
    private StatService statService;

    @Resource
    private TenantApi tenantApi;

    @GetMapping("/repairStat")
    @Operation(summary = "修复补充统计报表")
    public CommonResult<List<StatRespVO>> repairStat() {
        statService.repairStat();
        return success(null);
    }

    @GetMapping("/list")
    @Operation(summary = "获得租户外呼统计报表")
    @PreAuthorize("@ss.hasPermission('call:stat:query')")
    public CommonResult<List<StatRespVO>> getStatList(@Valid StatReqVO pageReqVO) {
        List<StatRespVO> pageResult = statService.getStatList(pageReqVO);
        return success(pageResult);
    }


    @GetMapping("/list-by-proxy")
    @Operation(summary = "获得租户外呼统计报表")
    public CommonResult<List<StatRespVO>> getStatListByProxy(@Valid StatReqVO pageReqVO) {
        if (Objects.isNull(pageReqVO.getTenantId())) {
            throw exception(new ErrorCode(100500, "必须指定要查询的企业"));
        }
        Long tenantId = pageReqVO.getTenantId();
        tenantApi.validateTenantIsProxy(tenantId, TenantContextHolder.getTenantId());
        return TenantUtils.execute(tenantId, () -> {
            List<StatRespVO> pageResult = statService.getStatList(pageReqVO);
            return success(pageResult);
        });
    }

    @GetMapping("/owner")
    @Operation(summary = "获取员工外呼统计报表")
    @PreAuthorize("@ss.hasPermission('call:stat:query')")
    public CommonResult<List<StatOwnerRespVO>> getStatOwner(@Valid StatOwnerReqVO ReqVO) {
        List<StatOwnerRespVO> pageResult = statService.getStatOwner(ReqVO);
        return success(pageResult);
    }

    @GetMapping("/owner-by-proxy")
    @Operation(summary = "获取员工外呼统计报表")
    public CommonResult<List<StatOwnerRespVO>> getStatOwnerByProxy(@Valid StatOwnerReqVO ReqVO) {

        if (Objects.isNull(ReqVO.getTenantId())) {
            throw exception(new ErrorCode(100500, "必须指定要查询的企业"));
        }
        Long tenantId = ReqVO.getTenantId();
        tenantApi.validateTenantIsProxy(tenantId, TenantContextHolder.getTenantId());
        return TenantUtils.execute(tenantId, () -> {
            List<StatOwnerRespVO> pageResult = statService.getStatOwner(ReqVO);
            return success(pageResult);
        });
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出租户外呼统计报表 Excel")
    @PreAuthorize("@ss.hasPermission('call:stat:export')")
    @OperateLog(type = EXPORT)
    public void exportStatExcel(@Valid StatReqVO ReqVO, HttpServletResponse response) throws IOException {
        List<StatRespVO> list = statService.getStatList(ReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "外呼统计报表.xls", "数据", StatRespVO.class, list);
    }

    @GetMapping("/export-excel-by-proxy")
    @Operation(summary = "导出租户外呼统计报表 Excel")
    @OperateLog(type = EXPORT)
    public void exportStatExcelByProxy(@Valid StatReqVO ReqVO, HttpServletResponse response) throws IOException {
        if (Objects.isNull(ReqVO.getTenantId())) {
            throw exception(new ErrorCode(100500, "必须指定要查询的企业"));
        }
        Long tenantId = ReqVO.getTenantId();
        List<StatRespVO> list = TenantUtils.execute(tenantId, () ->
                statService.getStatList(ReqVO));
        // 导出 Excel
        ExcelUtils.write(response, "外呼统计报表.xls", "数据", StatRespVO.class, list);
    }

    @GetMapping("/owner-export-excel")
    @Operation(summary = "导出坐席外呼统计报表 Excel")
    @PreAuthorize("@ss.hasPermission('call:stat:export')")
    @OperateLog(type = EXPORT)
    public void exportStatOwnerExcel(@Valid StatOwnerReqVO pageReqVO, HttpServletResponse response) throws IOException {
        List<StatOwnerRespVO> list = statService.getStatOwner(pageReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "坐席外呼统计报表.xls", "数据", StatOwnerRespVO.class, list);
    }

    @GetMapping("/owner-export-excel-by-proxy")
    @Operation(summary = "导出坐席外呼统计报表 Excel")
    @OperateLog(type = EXPORT)
    public void exportStatOwnerExcelByProxy(@Valid StatOwnerReqVO pageReqVO, HttpServletResponse response) throws IOException {
        if (Objects.isNull(pageReqVO.getTenantId())) {
            throw exception(new ErrorCode(100500, "必须指定要查询的企业"));
        }
        Long tenantId = pageReqVO.getTenantId();
        List<StatOwnerRespVO> list = TenantUtils.execute(tenantId, () ->
                statService.getStatOwner(pageReqVO));
        // 导出 Excel
        ExcelUtils.write(response, "坐席外呼统计报表.xls", "数据", StatOwnerRespVO.class, list);
    }

}